package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.MpTeamApply;
import com.ruoyi.db.domain.system.PcTeamConfig;
import com.ruoyi.db.mapper.system.MpTeamApplyMapper;
import com.ruoyi.db.mapper.system.PcTeamConfigMapper;
import com.ruoyi.mp.service.IMpTeamApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 【商城用户】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpTeamApplyServiceImpl implements IMpTeamApplyService
{
    @Autowired
    private MpTeamApplyMapper mpTeamApplyMapper;
    @Autowired
    private PcTeamConfigMapper pcTeamConfigMapper;

    /**
     * 查询【商城用户】
     * 
     * @param teamApplyId 【商城用户】主键
     * @return 【商城用户】
     */
    @Override
    public MpTeamApply selectMpTeamApplyByTeamApplyId(Long teamApplyId)
    {
        return mpTeamApplyMapper.selectMpTeamApplyByTeamApplyId(teamApplyId);
    }

    /**
     * 查询【商城用户】列表
     * 
     * @param mpTeamApply 【商城用户】
     * @return 【商城用户】
     */
    @Override
    public List<MpTeamApply> selectMpTeamApplyList(MpTeamApply mpTeamApply, MpClient client)
    {
        List<MpTeamApply> list = mpTeamApplyMapper.selectMpTeamApplyList(mpTeamApply);
        if(list!=null&&list.size()!=0){
            for (MpTeamApply teamApply:list) {
                teamApply.setClientName(client.getName());
                teamApply.setClientMobile(client.getMobile());
                if(teamApply.getApprovalStatus().equals("101")){
                    teamApply.setRefuseReason("审核中");
                }
                if(teamApply.getApprovalStatus().equals("201")){
                    teamApply.setRefuseReason("审核成功");
                }
                if(teamApply.getApprovalStatus().equals("301")){
                    teamApply.setRefuseReason(teamApply.getRefuseReason());
                }
            }
        }
        return list;
    }

    /**
     * 新增【商城用户】
     * 
     * @param mpTeamApply 【商城用户】
     * @return 结果
     */
    @Override
    public void insertMpTeamApply(MpTeamApply mpTeamApply)
    {
        MpTeamApply mpTeamApply1 = new MpTeamApply();
        mpTeamApply1.setClientId(mpTeamApply.getClientId());
        MpTeamApply newOne = mpTeamApplyMapper.selectMpTeamApplyNewList(mpTeamApply1);
        if(newOne!=null&&newOne.getApprovalStatus().equals("101")){
            throw new ServiceException("不能重复申请");
        }
        mpTeamApply.setApplyTime(new Date());
        mpTeamApply.setApprovalStatus("101");

        mpTeamApplyMapper.insertMpTeamApply(mpTeamApply);

    }

    @Override
    public List<PcTeamConfig> selectMpTeamList(PcTeamConfig teamConfig) {
        return pcTeamConfigMapper.selectPcTeamConfigList(teamConfig);
    }

}
